<template>
  <div>
    <!--筛查-->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="分类名称">
        <el-input v-model="queryParams.cthrName" placeholder="分类名称"></el-input>
      </el-form-item>
      <el-form-item label="分类序号">
        <el-input v-model.number="queryParams.cthrNumFrom" placeholder="从"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model.number="queryParams.cthrNumTo" placeholder="到"></el-input>
      </el-form-item>
      <br>
      <el-form-item label="分类代号">
        <el-input v-model="queryParams.cthrCode" placeholder="分类代号"></el-input>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="queryParams.ctwoId" placeholder="请选择二级分类">
          <el-option v-for="item in ctwoList" :key="item.ctwoId" :label="item.ctwoName"
                     :value="item.ctwoId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="一级分类">
        <el-checkbox-group v-model="nums">
          <el-checkbox v-for="item in coneList" :key="item.coneId" :label="item.coneId">{{ item.coneName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-plus" size="mini" @click="query">查询
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-plus" size="mini" @click="reSet">清除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" @click="addHandler()" size="mini">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" @click="deleteHandler()" size="mini">删除</el-button>
      </el-col>
    </el-row>
    <br>
    <!--查询数据-->
    <el-table
        :data="tableData"
        stripe v-loading="loading"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column prop="cthrId" label="三级分类主键" width="180"></el-table-column>
      <el-table-column prop="coneName" label="一级分类" width="180"></el-table-column>
      <el-table-column prop="ctwoName" label="二级分类"></el-table-column>
      <el-table-column prop="cthrName" label="分类名称"></el-table-column>
      <el-table-column prop="cthrNum" label="分类序号"></el-table-column>
      <el-table-column prop="cthrCode" label="分类代号"></el-table-column>
      <el-table-column prop="cthrInfo" label="分类说明"></el-table-column>
      <el-table-column class="caoZuo" label="操作" width="200px">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="editHandler(scope.row.cthrId)">修改</el-button>
          <el-button type="danger" size="mini" @click="deleteHandler(scope.row.cthrId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 50, 100]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <!--弹窗页-->
    <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="50%"
        append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">


        <el-row>
          <el-col :span="12">
            <el-form-item label="一级分类" prop="coneId">
              <el-select v-model.number="form.coneId">
                <el-option v-for="item in coneList" :key="item.coneId" :label="item.coneName"
                           :value="item.coneId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二级分类" prop="ctwoId">
              <el-select v-model="form.ctwoId">
                <el-option v-for="item in ctwoList" :key="item.ctwoId" :label="item.ctwoName"
                           :value="item.ctwoId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类名称" prop="cthrName">
              <el-input v-model="form.cthrName" placeholder="请输入分类名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类代号" prop="cthrCode">
              <el-input v-model="form.cthrCode" placeholder="请输入分类代号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="分类序号" prop="cthrNum">
          <el-input-number :min="0" v-model="form.cthrNum" placeholder="请输入分类序号"></el-input-number>
        </el-form-item>

        <el-form-item label="分类说明" prop="cthrInfo">
          <el-input type="textarea"
                    :rows="2"
                    placeholder="请输入内容"
                    v-model="form.cthrInfo"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit()">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {getPage,editPage,editSave,addSave,deleteIds} from "@/api/webshop/CategoryThree";
import {listCone} from "@/api/webshop/CategoryOne";
import {listCtwo} from "@/api/webshop/CategoryTwo";

export default {
  title: '',
  //用于删除的id集合
  ids: [],
  data() {
    return {
      total: 0,
      loading: false,
      nums: [],
      title: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        coneIds: '',
        ctwoId: null,
        cthrName: '',
        cthrNumFrom: null,
        cthrNumTo: null,
        cthrCode: ''
      },
      form: {
        cthrId: null,
        coneId: null,
        ctwoId: null,
        cthrName: '',
        cthrNum: null,
        cthrCode: '',
        cthrInfo: ''
      },
      rules: {
        cthrName: [
          {required: true, message: '请输入三级分类名称', trigger: 'blur'},
          {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
        ],
      },
      dialogVisible: false,
      formLabelWidth: '120px',
      tableData: [],
      coneList: [],
      ctwoList: [],
      coneFullList:[],
      ctwoFullList:[]
    }
  },
  watch:{
    "form.coneId":{
      handler(val) {
        console.log(val)
        if (val != null){
        this.ctwoList = this.ctwoFullList.filter( two => two.coneId == val )
        }else {
          this.ctwoList = this.ctwoFullList
        }
        this.form.ctwoId = null;
      }
    }
  },
  created() {
    this.getList()
    this.getConeList()
    this.getCtwoList()

  },
  methods: {
    getList() {
      this.loading = true
      // 写请求
      getPage(this.queryParams).then(resp => {
        this.tableData = resp.rows
        this.loading = false
        this.total = resp.total
      })
    },
    getCtwoList() {
      // 写请求
      listCtwo().then(resp => {
        this.ctwoFullList = resp
      })
    },
    getConeList() {
      // 写请求
      listCone().then(resp => {
        this.coneList = resp
      })
    }, /**
     * 清空表单
     */
    resetForm() {
      this.form = {
        coneIds: '',
        ctwoId: null,
        cthrName: '',
        cthrNumFrom: null,
        cthrNumTo: null,
        cthrCode: ''
      }
    },
    /**
     * 打开添加 对话框
     */
    addHandler() {
      this.resetForm()
      this.title = '添加信息'
      this.dialogVisible = true
    },
    /**
     * 打开修改 对话框
     */
    editHandler(id) {
      console.log(id)
      editPage(id).then(resp => {
        this.form = resp.data
        this.title = '修改信息'
        this.dialogVisible = true
      })
    },
    /**
     * 添加保存处理
     */
    onSubmit() {
      // 通过选择器找到form表单进行处理
      this.$refs['form'].validate((valid) => {
        if (valid) {
          //通过了校验
          if (this.form.cthrId) {
            // 修改
            // axios处理传值进行添加处理
            editSave(this.form).then(resp => {
              //判断成功还是失败,给予明确提示
              if (resp.code == 200) {
                //弹出成功消息
                this.$message({
                  message: '保存成功',
                  type: 'success'
                });
                // 关闭显示窗
                this.dialogVisible = false
                // 刷新页面
                this.getList()
              } else {
                // 弹出失败消息
                this.$message.error('保存失败');
              }
            })
          } else {
            // 添加
            // axios处理传值进行添加处理
            addSave(this.form).then(resp => {
              //判断成功还是失败,给予明确提示
              if (resp.code == 200) {
                //弹出成功消息
                this.$message({
                  message: '保存成功',
                  type: 'success'
                });
                // 关闭显示窗
                this.dialogVisible = false
                // 刷新页面
                this.getList()
              } else {
                // 弹出失败消息
                this.$message.error('保存失败');
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }


      });
    },
    /**
     * 删除处理
     * @param id
     */
    deleteHandler(id) {
      // console.log(id)
      //判断传入的是单条操作还是多选操作
      let idss = id || this.ids.join(',')
      // axios处理传值进行删除处理
      deleteIds(idss).then(resp => {
        //判断成功还是失败
        if (resp.code == 200) {
          //弹出成功消息
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          // 刷新页面
          this.getList()
        } else {
          // 弹出失败消息
          this.$message.error('删除失败');
        }
      }).catch(err => {
        // 发生异常时,弹出异常消息
        this.$message.error('删除异常');
      })
    },
    /**
     *     多选 选中处理
     */
    handleSelectionChange(val) {
      // console.log(val)
      this.ids = val.map(item => item.cthrId)
      console.log(this.ids)
    },
    handleSizeChange(val) {
      // console.log(val)
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      // console.log(val)
      this.queryParams.pageNum = val
      this.getList()
    },
    query() {
      console.log(nums)
      this.queryParams.coneIds = this.nums.join(',')
      // console.log(this.queryParams)
      this.getList()
    },
    reSet() {
      this.queryParams.coneIds = ''
      this.queryParams.ctwoId = null
      this.queryParams.cthrName = ''
      this.queryParams.cthrNumFrom = null
      this.queryParams.cthrNumTo = null
      this.queryParams.cthrCode = ''
      this.nums = []
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
    },
    logout() {
      this.$confirm('是否要退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$router.push({path: "/"});
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        });
      });

    }

  }
}
</script>

<style scoped lang="scss">
.el-form {
  margin: 20px auto;
  width: 90%;
  text-align: left;
}
</style>
